﻿using System;
using System.Collections.Specialized;
using System.Reflection;
using VitCode.ObjectResolutionHelper;

namespace VitCode.Castle.Entities
{
    [Serializable]
    public abstract class BaseEntity
    {
        public static string ID = "Id";
        protected string m_Memo;

        /// <summary>
        /// 数据主键
        /// </summary>
        [OrMemo("Id")]
        public virtual Guid Id
        {
            get;
            set;
        }

        /// <summary>
        /// 备注
        /// </summary>
        [OrMemo("备注")]
        public virtual string Memo
        {
            get { return m_Memo; }
            set { m_Memo = value; }
        }

        [OrMemo("当前对应的历史记录Id")]
        public virtual Guid CurrHisId { get; set; }

        public virtual StringCollection GetNotCompareHisProNames()
        {
            return new StringCollection {ID,};
        }

        public virtual StringCollection GetSysProNames()
        {
            return new StringCollection {ID, "CurrHisId"};
        }

        public virtual StringCollection GetKeyProNames()
        {
            return new StringCollection {"Name"};
        }


        public virtual StringCollection GetUniqueComposeProNames()
        {
            PropertyInfo p = GetType().GetProperty("Name");
            if (p != null)
                return new StringCollection {p.Name};
            p = GetType().GetProperty("Code");
            if (p != null)
                return new StringCollection {p.Name};
            else
                throw new ArgumentException("找不到业务唯一主键,默认[Name]或[Code]");
        }

        public static string GetImportNum()
        {
            return DateTime.Now.ToString("yyyyMMddhhmmssffff");
        }

        public virtual void SetSysExPro()
        {
        }

        public virtual void SetSysExProAfterSave()
        {
            PropertyInfo pPro = GetType().GetProperty("Parent");
            PropertyInfo orderIdPro = GetType().GetProperty("OrderId");
            if (orderIdPro != null && orderIdPro.PropertyType == typeof (string) && pPro != null &&
                pPro.PropertyType == GetType())
            {
                object pProVal = pPro.GetValue(this, null);
                if (pProVal != null)
                {
                    string pOrderId = orderIdPro.GetValue(pProVal, null).ToString();
                    orderIdPro.SetValue(this, pOrderId + "." + Id, null);
                }
                else
                {
                    orderIdPro.SetValue(this, Id.ToString(), null);
                }
            }
        }
    }
}